const router = require('koa-router')()

//引入用户类型
// const { Users, Menus } = require('../models')
//状态
const { SussessModel, FailModel } = require('../extend/helper')
router.prefix('/users')

router.get('/', function (ctx, next) {
  ctx.body = 'this is a users response!'
})

router.get('/bar', function (ctx, next) {
  ctx.body = '我是测试'
})

router.post("/register",async (ctx,next)=>{
  const { username, password } = ctx.request.body;  
  const regRes= await find({username})
  if (!regRes.length) {
    const res = await create({ username, password })
    ctx.body = new SussessModel('success', res, 200)
  } else {
    ctx.body = new FailModel('用户已经存在')
  }
})
// 登录
router.post("/login",async (ctx,next)=>{
    ctx.body = '登录成功'
  const { username, password } = ctx.request.body;  
  const loglist= await find({username})
  if (!loglist.length) {
    const res = await create({ username, password })
    ctx.body = new SussessModel('success', res, 200)
  } else {
    ctx.body = new FailModel('用户已经存在')
  }
})

module.exports = router
